
<template>
    <!-- h3要放在里面:只能有一个根,且title也是表单的一部分 -->
    <!-- prop对应rules里的键 -->
    <el-form :model="ruleForm" class="reg_container" status-icon :rules="rules" ref="ruleForm"  label-width="70px">
      <h3 class="register_title">用户注册</h3>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="设置密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pwd" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
    <el-form-item>
      <el-button @click="submitForm('ruleForm')" type="primary" style="margin-left:50px;margin-top:10px">注册</el-button>
    </el-form-item>
      <div class="re-top">
<!--        <p class="p2">我已注册，现在就</p>-->
        <router-link class="p2" to="/login" >我已注册，现在就登录</router-link>
<!--        <el-button type="primary"  style="margin-left:5px;height: 40px;background-color: #cccccc;" @click="gologin">登录</el-button>-->
      </div>
  </el-form>
</template>




<script >
import {register} from '@/api'
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pwd) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return{
      ruleForm: {
        username:'',
        pwd: '',
        checkPass: '',

      },
      rules: {
        pwd: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }

    };
  },
  computed:{

  userinfor(){
    return{
          number:this.ruleForm.username,
          pass:this.ruleForm.pwd
    }

  }

  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
       register({...this.userinfor}).then((data) =>{
         if(data.data.code===200){
           this.$message({
             message: '注册成功',
             type: 'success'
           });
           this.$router.push('/login')
         }else{
           this.$message.error(data.data.msg);
         }
          })


        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },


  }


}
</script>

<style lang="less" scoped>
.reg_container{
  width: 400px;
  border: 1px solid #eaeaea;
  margin: 180px auto;
  padding: 35px 35px 15px 35px;
  // 让padding在width里面
  box-sizing: border-box;
  border-radius: 15px;
  background-color: #fff;
  box-shadow: 0 0 25px #cac6c6;

  .register_title {
    color: #505458;
    // 左右居中
    text-align: center;
    margin-bottom: 40px;
  }

  .el-input {
    width: 198px;
  }
  .re-top {
    height: 60px;
    display: flex;
    align-items: center;
    //padding-left: 150px;
    width: 100%;
    padding: 0;
.p2{

  margin-left: 90px;

}






  }
}
</style>